<template>
  <div>
    <div class="search" style="margin: 0 0 0 30px;">
      <!-- {{ form }} -->
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-row>
          <el-col :span="18">
            <el-form-item>
              <el-select clearable v-model="form.categoryId" placeholder="请选择栏目分类">
                <el-option v-for="item in categories" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input clearable v-model="form.title" placeholder="请输入公告标题"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button @click="toPublish" type="primary" margin="right">发布公告</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="table">
      <el-table element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)" :data="categoryData" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" align="center">
        </el-table-column>
        <el-table-column label="公告标题" width="500" align="center">
          <template v-slot="scope">
            <div>
              <!-- {{ scope.row }} -->
              <div>{{ scope.row.title }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="no" label="作者" width="200" align="center">
          <template v-slot="scope">
            <div>
              <div>{{ scope.row.author }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">

          <template v-slot="scope">
            <div>
              <el-link @click="toSee(scope.row)" style="font-size: 13px; margin-right: 5px;" type="info"
                :underline="false">查看</el-link>
              <el-link @click="toDelete(scope.row)" style="font-size: 13px; margin-right: 5px;" type="danger"
                :underline="false">删除</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { get, post } from '@/utils/request'

export default {
  data() {
    return {
      form: {},
      categories: [],
      categoryData: [],
      params: {
        page: 1,
        pageSize: 15,
      },
      dialogVisible: false
    }
  },
  methods: {
    // 删除
    toDelete(id) {
      console.log(id.id);
      this.$confirm('此操作将删除此公告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await get('/article/deleteArticle', {id:id.id})
        if (res.status == 200) {
          this.$notify({
            title: res,message,
            message: '删除成功',
            type: 'success' 
          })
          this.getArticles()
        } else {
          this.$notify.error({
            title: '失败',
            message: '删除失败',
            type: 'success'
          })
        }
      }).catch(() => {
        this.$notify({
          title: '取消删除',
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 获取栏目
    async getCategories() {
      let res = await get('/category/findAll')
      // console.log(res);
      this.categories = res.data
    },
    async getArticles() {
      let temp = {
        ...this.form,
        ...this.params
      }
      // 判断条件是否存在 存在传给后端  不存在删除条件
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key]
        }
      }
      let res = await get('category/pageQueryFind', temp)
      // console.log(res);
      this.categoryData = res.data.list
    },
    // 去查看详情
    toSee(articleDetails) {
      //  跳转到主页
      this.$router.push({
        path: "/detail/index",
        query: articleDetails
      })
    },
    toPublish() {
      this.$router.push({
        path: "/manage/publish",
      })
    },
  },
  // 监听form
  watch: {
    // form(){
    //   this.getArticle()
    // }
    form: {
      handler() {
        this.params.page = 1;
        this.getArticles()
      },
      deep: true
    }
  },
  created() {
    this.getCategories(),
      this.getArticles()
  },
}
</script>